﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

namespace SocketLib
{
	public class Async_Send_Receive
	{

		public delegate void CallbackDelegate(object sender, CallbackEventArgs e);
		public event CallbackDelegate CallbackEventHandler;


		//public  static Read_Callback(IAsyncResult ar)
		//{
		//    StateObject so = (StateObject)ar.AsyncState;
		//    Socket s = so.workSocket;

		//    int read = s.EndReceive(ar);

		//    if (read > 0)
		//    {
		//        so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read));

		//        if (CallbackEventHandler != null)
		//        {
		//            CallbackEventArgs args = new CallbackEventArgs();
		//            args.msg = Encoding.ASCII.GetString(so.buffer, 0, read);
		//            CallbackEventHandler(this, args);
		//        }


		//        s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0,
		//                                 new AsyncCallback(Async_Send_Receive.Read_Callback), so);
		//    }
		//    else
		//    {
		//        if (so.sb.Length > 1)
		//        {
		//            //All of the data has been read, so displays it to the console
		//            string strContent;
		//            strContent = so.sb.ToString();
		//            Console.WriteLine(String.Format("Read {0} byte from socket" +
		//                             "data = {1} ", strContent.Length, strContent));
		//        }
		//        s.Close();
		//    }
		//}






	}

	public class StateObject
	{
		public Socket workSocket = null;
		public const int BUFFER_SIZE = 1024;
		public byte[] buffer = new byte[BUFFER_SIZE];
		public StringBuilder sb = new StringBuilder();
	}


	public class CallbackEventArgs : EventArgs
	{

		public string msg { get; set; }
	}




}
